using RuoVea.DynamicWebApi;
using RuoVea.ExFilter;
using RuoVea.ExJwtBearer;
using RuoVea.ExSugar;
using RuoVea.ExSwagger;
using RuoVea.OmiApi.Config;
using RuoVea.OmiApi.Auth;
using RuoVea.OmiApi.Log;
using RuoVea.OmiApi.UserRoleMenu;
using RuoVea.ExConfig;

namespace RuoVea.OMenuRoleUser.Template
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("AllowSettingCors", builder =>
                {
                    builder.SetIsOriginAllowed(origin =>
                    {
                        var allowedOrigins = AppSettings.app("Cors", "CorsUrls").Split(',').Select(url => url.Trim()).ToList();
                        return allowedOrigins.Contains(origin);
                    })
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
                });
            });
            builder.Services.AddControllers();

            /*类库映射API组件*/
            if (builder.Environment.IsProduction())
            {
                builder.Services.AddDynamicWebApi(options =>
                {
                    options.DefaultApiPrefix = "/openapi/api";
                    options.RemoveControllerPostfixes = new List<string> { "AppService", "Service" };
                    options.RemovePrefix = new List<string> { "get", "post" };
                });
            }
            else
            {
                builder.Services.AddDynamicWebApi(options =>
                {
                    options.RemoveControllerPostfixes = new List<string> { "AppService", "Service" };
                    options.RemovePrefix = new List<string> { "get", "post" };
                });
            }

            builder.Services.AddOmiSystemSetup();

            builder.Services
                .AddOmiConfigSetup()
                .AddOmiLogSetup()
                .AddOmiSystemSetup()
                .AddOmiAuthSetup();

            builder.Services.AddHttpContextSetup<AspNetUser>();
            builder.Services.AddAuthenticationSetup(IdentifyEnum.Jwt, true);

            builder.Services
                .AddSqlSugarSetup();

            builder.Services
                .AddConfigInitSetup()
                .AddLogInitSetup()
                .AddAuthInitSetup()
                .AddSystemInitSetup();/*需要在 AddSqlSugarSetup之后*/
            //builder.Services.AddDictInitSetup();/*需要在 AddSqlSugarSetup之后*/


            builder.Services
                .RequestActionSetup() // 注入 请求日志拦截 [执行操作日志、参数验证 ]
                .ResultSetup()
                .ExceptionSetup();

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();

            builder.Services.AddSwaggerSetup(true);

            var app = builder.Build();

            app.UseCors("AllowSettingCors");

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwaggerMiddle(builder.Environment);
            }

            app.UseAuthentication();
            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}
